package com.XZDP.config;

import com.XZDP.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.mail.MessagingException;

@Slf4j
@RestControllerAdvice
@ControllerAdvice
public class WebExceptionAdvice {

    @ExceptionHandler(RuntimeException.class)
    public Result handleRuntimeException(RuntimeException e) {
        log.error(e.toString(), e);
        return Result.fail("服务器异常");
    }

    @ExceptionHandler(MessagingException.class)
    @ResponseBody
    public Result handleMessagingException(MessagingException ex) {
        // 记录异常日志
        ex.printStackTrace();
        // 返回友好的错误信息
        return Result.fail("邮件发送失败，请稍后再试");
    }
}
